home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7133 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news-m01.ny.us.ibm.net!usenet
  2. From: bbogard@ibm.net
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual Constructors (how to implement)?
  5. Date: 21 Feb 1996 20:56:53 GMT
  6. Message-ID: <4gg0ul$401m@news-s01.ny.us.ibm.net>
  7. References: <4g0lle$nmq@jobe.shell.portal.com>
  8. Reply-To: bbogard@ibm.net
  9. NNTP-Posting-Host: slip37-223-98.ibm.net
  10. X-Newsreader: IBM NewsReader/2 v1.2.5
  11.  
  12. In <4g0lle$nmq@jobe.shell.portal.com>, clare@shell.portal.com (Clare Chu) writes:
  13. >
  14. >I have an abstract base class Packet.  Inherited from Packet
  15. >are specific packets like LoginPacket, AuditPacket, etc.
  16. >
  17. >What I need is to read a socket (stream), and then construct
  18. >the packet of the right kind, depending on what it is.
  19. >
  20. >I read a reference to this technique in Scott Meyer's More Effective C++
  21. >but I haven't received my copy yet.  (It is back-ordered at Computer
  22. >Literacy).
  23. >
  24. >I need someone to basically outline what I need to do to set
  25. >up such a function that reads from a stream, produces the appropriate
  26. >object and returns a pointer to Packet pointing to the object
  27. >which is of the appropriate derived class.
  28. >
  29. >At present, I'm trying to implement it as an array of pointers
  30. >to functions.  Those functions will be called based on the
  31. >Packet type, and create the appropriate object.  Of course I
  32. >haven't even thought about going out of scope, destructors,
  33. >or such.  My coworker says to just use a big switch statement
  34. >and construct objects based on the type.  
  35. >
  36. >Any ideas?
  37. >
  38. >Thanks in advance,
  39. >Clare
  40. >clare@shell.portal.com
  41. >
  42. >
  43. >
  44.  
  45. You could create an Abstract Factory type of object that given a certain object number
  46. and input stream, would create the proper object.  That way all the creation of the objects
  47. would be centralized.
  48.  
  49.